<template>
<div id="login">
    <div class="login-logo">
        <img src="../../assets/logo.png" alt="">
    </div>
    <form class="form-info">
        <label for="username">
            <span>用户名&nbsp;</span>
            <input 
                type="text" 
                id="username" 
                class="username" 
                v-model="username"
                required
                placeholder="请输入账号"
            >
        <div class="form-tip">
            <p :class="uflag ? 'tip-err' : 'tip-succ' ">{{ucontent}}</p>
        </div>
        </label>
        <label for="password">
            <span>密码&nbsp;</span>
            <input 
                type="password" 
                id="password" 
                class="password" 
                v-model="password"
                required
                placeholder="请输入密码"
            >
        <div class="form-tip">
            <p :class="pflag ? 'tip-err' : 'tip-succ' ">{{pcontent}}</p>
        </div>
        </label>
    </form>
    <my-button
        :type="'primary'"
        @click.native="handleLogin"
        :disabled="uflag || pflag"
    >登录</my-button>
</div>
</template>

<script>
import { Local } from "../../common"
import { login } from "../../config/data"
import { TOKEN } from "../../config/localConstant"
export default {
    name: "login",
    data() {
        return {
            username: "",
            password: "",
            ucontent: "",
            pcontent: "",
            uflag: true,
            pflag: true,
        }
    },
    mounted() {
        this.username = "admin"
        this.password = "123456"
    },
    methods: {
        async handleLogin() {
            let result = await login({
                username: this.username,
                password: this.password
            })
            if(result.data) {
                // 登录，存储 token
                Local.set(TOKEN, result.data.token)
                this.$router.push("/")
            }
        }
    },
    watch: {
        "username"(newVal) {
            this.username = this.username.trim()
            let len = newVal.trim().length
            if(len === 0) this.uflag = false
            if(len >= 3 && len <= 10) {
                this.uflag = false
                this.ucontent = `正确`
            } else {
                this.uflag = true
                this.ucontent = `用户名长度在 3 到 10 个字符`
            }
        },
        "password"(newVal) {
            this.password = this.password.trim()
            let len = newVal.trim().length
            if(len === 0) this.pflag = false
            if(len >= 6 && len <= 15) {
                this.pflag = false
                this.pcontent = `正确`
            } else {
                this.pflag = true
                this.pcontent = `用户名长度在 6 到 15 个字符`
            }
        }
    },
}
</script>

<style scoped>
#login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    max-width: 375px;
    margin: auto;
    padding-top: 15px;
    padding-bottom: 30px;
    background-color: #e4e4e4;
    transform: translateY(38%);
    border-radius: 6px;
    box-shadow: 0 0 50px 10px #e4e4e4;
    box-sizing: border-box;
}
.login-logo, .form-info {
    margin: 5px 0;
}
.login-logo {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background-color:rgba(207, 207, 207, 0.5);
    box-shadow: 0 0 50px #cfcfcf80;
}
.login-logo img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: #fff;
}
.form-info {
    display: flex;
    flex-direction: column;
    white-space: nowrap;
}
.form-info span {
    display: inline-block;
    min-width: 80px;
    height: 38px;
    line-height: 38px;
    margin-right: 12px;
    text-align-last: justify;
    font-weight: 600;
}
.form-info .username, .form-info .password {
    display: inline-block;
    padding: 6px 12px;
    border: none;
    outline: none;
    font-size: 16px;
    background-color: #969696;
    border-radius: 6px;
    transition: .4s;
}
.form-info .username:hover, .form-info .password:hover,
.form-info .username:focus, .form-info .password:focus {
    background-color: #c0c0c0;
}
.form-info .form-tip {
    height: 20px;
    text-align: right;
    font-size: 14px;
    font-weight: 600;
}
.form-info .form-tip .tip-succ {
    color: #5abb57;
}
.form-info .form-tip .tip-err {
    color: #ac3030;
}
.btn-submit {
    width: 300px;
    height: 42px;
    margin-top: 12px;
    border: none;
    outline: none;
    border-radius: 6px;
    background-color: #2752a0;
    color: #ccc;
    font-size: 16px;
    font-weight: 600;
    transition: .4s;
    cursor: pointer;
}
.btn-submit:hover {
    background-color: #346bd1;
    color: #a7a7a7;
}
</style>
